package cn.itcast.bos.service.transit.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.transit.InOutStorageInfoRepository;
import cn.itcast.bos.dao.transit.TransitInfoRepository;
import cn.itcast.bos.domain.transit.InOutStorageInfo;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.service.transit.InOutStorageInfoService;

@Service
@Transactional
public class InOutStorageInfoServiceImpl implements InOutStorageInfoService {

	@Autowired
	private InOutStorageInfoRepository inOutStorageInfoRepository;

	@Autowired
	private TransitInfoRepository transitInfoRepository;

	@Override
	public void save(String transitInfoId, InOutStorageInfo inOutStorageInfo) {
		// 保存中转信息到到数据库表中
		inOutStorageInfoRepository.save(inOutStorageInfo);

		// 查询运输记录信息
		TransitInfo transitInfo = transitInfoRepository
				.findOne(Integer.parseInt(transitInfoId));
		//关联出入库信息到运输记录中
		transitInfo.getInOutStorageInfos().add(inOutStorageInfo);
		System.out.println("关联到运输记录中出入库信息的id是 :"+ inOutStorageInfo.getId());
		
		//出入库信息状态为'到达网点'时,需要对运输信息状态进行更改
		if("到达网点".equals(inOutStorageInfo.getOperation())) {
			transitInfo.setStatus("到达网点");
			transitInfo.setOutletAddress(inOutStorageInfo.getAddress());
		}
		
	}

}
